{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 8.21 \u5b9e\u73b0\u8bbf\u95ee\u8005\u6a21\u5f0f\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u8981\u5904\u7406\u7531\u5927\u91cf\u4e0d\u540c\u7c7b\u578b\u7684\u5bf9\u8c61\u7ec4\u6210\u7684\u590d\u6742\u6570\u636e\u7ed3\u6784\uff0c\u6bcf\u4e00\u4e2a\u5bf9\u8c61\u90fd\u9700\u8981\u8fdb\u884c\u4e0d\u540c\u7684\u5904\u7406\u3002\n\u6bd4\u5982\uff0c\u904d\u5386\u4e00\u4e2a\u6811\u5f62\u7ed3\u6784\uff0c\u7136\u540e\u6839\u636e\u6bcf\u4e2a\u8282\u70b9\u7684\u76f8\u5e94\u72b6\u6001\u6267\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u91cc\u9047\u5230\u7684\u95ee\u9898\u5728\u7f16\u7a0b\u9886\u57df\u4e2d\u662f\u5f88\u666e\u904d\u7684\uff0c\u6709\u65f6\u5019\u4f1a\u6784\u5efa\u4e00\u4e2a\u7531\u5927\u91cf\u4e0d\u540c\u5bf9\u8c61\u7ec4\u6210\u7684\u6570\u636e\u7ed3\u6784\u3002\n\u5047\u8bbe\u4f60\u8981\u5199\u4e00\u4e2a\u8868\u793a\u6570\u5b66\u8868\u8fbe\u5f0f\u7684\u7a0b\u5e8f\uff0c\u90a3\u4e48\u4f60\u53ef\u80fd\u9700\u8981\u5b9a\u4e49\u5982\u4e0b\u7684\u7c7b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class Node:\n    pass\n\nclass UnaryOperator(Node):\n    def __init__(self, operand):\n        self.operand = operand\n\nclass BinaryOperator(Node):\n    def __init__(self, left, right):\n        self.left = left\n        self.right = right\n\nclass Add(BinaryOperator):\n    pass\n\nclass Sub(BinaryOperator):\n    pass\n\nclass Mul(BinaryOperator):\n    pass\n\nclass Div(BinaryOperator):\n    pass\n\nclass Negate(UnaryOperator):\n    pass\n\nclass Number(Node):\n    def __init__(self, value):\n        self.value = value"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7136\u540e\u5229\u7528\u8fd9\u4e9b\u7c7b\u6784\u5efa\u5d4c\u5957\u6570\u636e\u7ed3\u6784\uff0c\u5982\u4e0b\u6240\u793a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Representation of 1 + 2 * (3 - 4) / 5\nt1 = Sub(Number(3), Number(4))\nt2 = Mul(Number(2), t1)\nt3 = Div(t2, Number(5))\nt4 = Add(Number(1), t3)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u6837\u505a\u7684\u95ee\u9898\u662f\u5bf9\u4e8e\u6bcf\u4e2a\u8868\u8fbe\u5f0f\uff0c\u6bcf\u6b21\u90fd\u8981\u91cd\u65b0\u5b9a\u4e49\u4e00\u904d\uff0c\u6709\u6ca1\u6709\u4e00\u79cd\u66f4\u901a\u7528\u7684\u65b9\u5f0f\u8ba9\u5b83\u652f\u6301\u6240\u6709\u7684\u6570\u5b57\u548c\u64cd\u4f5c\u7b26\u5462\u3002\n\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528\u8bbf\u95ee\u8005\u6a21\u5f0f\u53ef\u4ee5\u8fbe\u5230\u8fd9\u6837\u7684\u76ee\u7684\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class NodeVisitor:\n    def visit(self, node):\n        methname = 'visit_' + type(node).__name__\n        meth = getattr(self, methname, None)\n        if meth is None:\n            meth = self.generic_visit\n        return meth(node)\n\n    def generic_visit(self, node):\n        raise RuntimeError('No {} method'.format('visit_' + type(node).__name__))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u4f7f\u7528\u8fd9\u4e2a\u7c7b\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u7ee7\u627f\u5b83\u5e76\u4e14\u5b9e\u73b0\u5404\u79cd visit_Name() \u65b9\u6cd5\uff0c\u5176\u4e2dName\u662fnode\u7c7b\u578b\u3002\n\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u6c42\u8868\u8fbe\u5f0f\u7684\u503c\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class Evaluator(NodeVisitor):\n    def visit_Number(self, node):\n        return node.value\n\n    def visit_Add(self, node):\n        return self.visit(node.left) + self.visit(node.right)\n\n    def visit_Sub(self, node):\n        return self.visit(node.left) - self.visit(node.right)\n\n    def visit_Mul(self, node):\n        return self.visit(node.left) * self.visit(node.right)\n\n    def visit_Div(self, node):\n        return self.visit(node.left) / self.visit(node.right)\n\n    def visit_Negate(self, node):\n        return -node.operand"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528\u793a\u4f8b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "e = Evaluator()\ne.visit(t4)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f5c\u4e3a\u4e00\u4e2a\u4e0d\u540c\u7684\u4f8b\u5b50\uff0c\u4e0b\u9762\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u5728\u4e00\u4e2a\u6808\u4e0a\u9762\u5c06\u4e00\u4e2a\u8868\u8fbe\u5f0f\u8f6c\u6362\u6210\u591a\u4e2a\u64cd\u4f5c\u5e8f\u5217\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class StackCode(NodeVisitor):\n    def generate_code(self, node):\n        self.instructions = []\n        self.visit(node)\n        return self.instructions\n\n    def visit_Number(self, node):\n        self.instructions.append(('PUSH', node.value))\n\n    def binop(self, node, instruction):\n        self.visit(node.left)\n        self.visit(node.right)\n        self.instructions.append((instruction,))\n\n    def visit_Add(self, node):\n        self.binop(node, 'ADD')\n\n    def visit_Sub(self, node):\n        self.binop(node, 'SUB')\n\n    def visit_Mul(self, node):\n        self.binop(node, 'MUL')\n\n    def visit_Div(self, node):\n        self.binop(node, 'DIV')\n\n    def unaryop(self, node, instruction):\n        self.visit(node.operand)\n        self.instructions.append((instruction,))\n\n    def visit_Negate(self, node):\n        self.unaryop(node, 'NEG')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528\u793a\u4f8b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s = StackCode()\ns.generate_code(t4)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u521a\u5f00\u59cb\u7684\u65f6\u5019\u4f60\u53ef\u80fd\u4f1a\u5199\u5927\u91cf\u7684if/else\u8bed\u53e5\u6765\u5b9e\u73b0\uff0c\n\u8fd9\u91cc\u8bbf\u95ee\u8005\u6a21\u5f0f\u7684\u597d\u5904\u5c31\u662f\u901a\u8fc7 getattr() \u6765\u83b7\u53d6\u76f8\u5e94\u7684\u65b9\u6cd5\uff0c\u5e76\u5229\u7528\u9012\u5f52\u6765\u904d\u5386\u6240\u6709\u7684\u8282\u70b9\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def binop(self, node, instruction):\n    self.visit(node.left)\n    self.visit(node.right)\n    self.instructions.append((instruction,))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd8\u6709\u4e00\u70b9\u9700\u8981\u6307\u51fa\u7684\u662f\uff0c\u8fd9\u79cd\u6280\u672f\u4e5f\u662f\u5b9e\u73b0\u5176\u4ed6\u8bed\u8a00\u4e2dswitch\u6216case\u8bed\u53e5\u7684\u65b9\u5f0f\u3002\n\u6bd4\u5982\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5199\u4e00\u4e2aHTTP\u6846\u67b6\uff0c\u4f60\u53ef\u80fd\u4f1a\u5199\u8fd9\u6837\u4e00\u4e2a\u8bf7\u6c42\u5206\u53d1\u7684\u63a7\u5236\u5668\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class HTTPHandler:\n    def handle(self, request):\n        methname = 'do_' + request.request_method\n        getattr(self, methname)(request)\n    def do_GET(self, request):\n        pass\n    def do_POST(self, request):\n        pass\n    def do_HEAD(self, request):\n        pass"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8bbf\u95ee\u8005\u6a21\u5f0f\u4e00\u4e2a\u7f3a\u70b9\u5c31\u662f\u5b83\u4e25\u91cd\u4f9d\u8d56\u9012\u5f52\uff0c\u5982\u679c\u6570\u636e\u7ed3\u6784\u5d4c\u5957\u5c42\u6b21\u592a\u6df1\u53ef\u80fd\u4f1a\u6709\u95ee\u9898\uff0c\n\u6709\u65f6\u5019\u4f1a\u8d85\u8fc7Python\u7684\u9012\u5f52\u6df1\u5ea6\u9650\u5236(\u53c2\u8003 sys.getrecursionlimit() )\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53ef\u4ee5\u53c2\u71678.22\u5c0f\u8282\uff0c\u5229\u7528\u751f\u6210\u5668\u6216\u8fed\u4ee3\u5668\u6765\u5b9e\u73b0\u975e\u9012\u5f52\u904d\u5386\u7b97\u6cd5\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8ddf\u89e3\u6790\u548c\u7f16\u8bd1\u76f8\u5173\u7684\u7f16\u7a0b\u4e2d\u4f7f\u7528\u8bbf\u95ee\u8005\u6a21\u5f0f\u662f\u975e\u5e38\u5e38\u89c1\u7684\u3002\nPython\u672c\u8eab\u7684 ast \u6a21\u5757\u503c\u5f97\u5173\u6ce8\u4e0b\uff0c\u53ef\u4ee5\u53bb\u770b\u770b\u6e90\u7801\u3002\n9.24\u5c0f\u8282\u6f14\u793a\u4e86\u4e00\u4e2a\u5229\u7528 ast \u6a21\u5757\u6765\u5904\u7406Python\u6e90\u4ee3\u7801\u7684\u4f8b\u5b50\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}